<template>
  <div class="dialog">
    <el-dialog
      :title="titleText"
      :visible.sync="showDialog"
      width="45%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="产线名称:" prop="productionLineName" class="w100p">
              <el-input
                v-model="formData.productionLineName"
                placeholder="请输入产线名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联设备:" prop="lineEquMapperList" class="w100p">
              <el-select
                v-model="formData.lineEquMapperList"
                placeholder="请选择"
                multiple
                :disabled="disable"
              >
                <el-option
                  v-for="item in equipTypeOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="关联加工产品:" prop="materialNo" class="w100p">
              <el-select v-model="formData.materialNo" :disabled="disable" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生成加工作业名称:" prop="operationName" class="w100p">
              <el-input
                v-model="formData.operationName"
                placeholder="请输入加工作业名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="标准作业程序文件:" prop="pictureNo" class="w100p">
            <el-upload
                ref="upload"
                class="upload-file"
                action
                :limit="1"
                multiple
                :on-remove="params => removeDocument(params)"
                :http-request="params => addDocument(params)"
                :file-list="DocumentList"
                accept=".png, .jpg, .jpeg, .pdf, .doc, .docx"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png/jpeg/pdf/doc/docx文件，且不超过400MB</div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from "@/utils/index";
import CommonFileApi from "@/api/common-file/index";
export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    options: {
      type: Array,
      default: () => []
    },
    equipTypeOptions: {
      type: Array,
      default: () => []
    },
    editData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      showDialog: this.show,
      // 展示
      formData: {},
      rules: {
        productionLineName: [
          {
            required: true,
            message: "请输入产线名称",
            trigger: ["change", "blur"]
          }
        ],
        lineEquMapperList: [
          {
            required: true,
            message: "请选择关联设备",
            trigger: ["change", "input", "blur"]
          }
        ],
        materialNo: [
          {
            required: true,
            message: "请选择关联产品",
            trigger: ["change", "blur"]
          }
        ],
        operationName: [
          {
            required: true,
            message: "请输入加工作业名称",
            trigger: ["change", "blur"]
          }
        ],
      },
      DocumentList:[],
      disable:false
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    titleText(val) {
      if(val === '编辑产线'){
        this.disable = true
      }
    },
  },
  created() {},
  mounted() {
    this.getformData();
    this.$nextTick(() => {
      this.$refs.ruleForm.clearValidate();
    });
  },
  methods: {
    getformData() {
      this.formData = deepClone(this.editData);
    },
    // 删除列表文件
    removeDocument(file) {
      const index = this.DocumentList.findIndex(item => {
        return item.uid === file.uid;
      });
      this.DocumentList.splice(index, 1);
    },
    // 添加列表文件
    async addDocument(file) {
      if (file.size / 1024 / 1024 > 400) {
        this.$message.error({
          message: `上传文件大小不能超过400M!`,
        });
        this.$refs.upload.clearFiles()
        return false;
      }
      let fileName = file.name
      let pos = fileName.lastIndexOf('.')
      let lastName = fileName.substring(pos, fileName.length)
      if (
        lastName.toLowerCase() !== '.jpg' &&
        lastName.toLowerCase() !== '.png' &&
        lastName.toLowerCase() !== '.jpeg' &&
        lastName.toLowerCase() !== '.pdf' &&
        lastName.toLowerCase() !== '.doc' &&
        lastName.toLowerCase() !== '.docx' 
      ) {
        this.$message.error('文件必须为.jpg .png .jpeg .pdf .doc .docx类型')
        this.$refs.upload.clearFiles()
        return false
      }
      this.DocumentList.push(file.file);
    },
    save(formData) {
      let self = this;
      this.$refs[formData].validate(async valid => {
        if (valid) {
          if (this.DocumentList.length > 0) {
            let form = new FormData();
            let upDownType = 'FTP';
            let moduleType = 'PROCESSTEST';
            form.append("file", this.DocumentList[0]);
            let res = await CommonFileApi.uploadFile(upDownType,moduleType,form);
            if (res.code !== 1) {
              this.$message.error("文件保存失败");
              return;
            }
            let fileId = res.data.fileId
            this.formData.fileIdList.push(fileId);
          }
          let list = [];
          self.formData.lineEquMapperList.forEach(e => {
            for (let i = 0; i < self.equipTypeOptions.length; i++) {
              if (e === self.equipTypeOptions[i].value) {
                list.push({
                  equipmentCode: self.equipTypeOptions[i].value,
                  equipmentName: self.equipTypeOptions[i].name
                });
              }
            }
          });
          this.formData.lineEquMapperList = list;
          this.$emit("addEvent", this.formData);
          this.close();
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("closeDialog");
      this.formData = {};
    }
  }
};
</script>

  <style lang="scss" scoped>
::v-deep
  .el-dialog__wrapper
  .el-dialog
  .el-dialog__body
  .el-form
  .el-form-item
  .el-form-item__label {
  min-width: 160px !important;
}
</style>
